package edu.neu.chenhan.fragment.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import java.util.ArrayList;
import java.util.List;
import edu.neu.chenhan.R;
import edu.neu.chenhan.adapter.NewsAdapter;
import edu.neu.chenhan.model.News;

public class NewsListFragment extends Fragment {

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView newsRecyclerView;
    private NewsAdapter newsAdapter;
    private List<News> newsList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_industry_news, container, false);

        swipeRefreshLayout = view.findViewById(R.id.swipe_refresh);
        newsRecyclerView = view.findViewById(R.id.news_recycler_view);

        // 初始化新闻数据
        initNewsData();

        // 设置 RecyclerView 的布局管理器
        newsRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        // 初始化适配器
        newsAdapter = new NewsAdapter(requireContext()); // 传递 Context 参数
        newsRecyclerView.setAdapter(newsAdapter);

        // 设置新闻列表数据
        newsAdapter.setNewsList(newsList);

        // 设置下拉刷新监听器
        swipeRefreshLayout.setOnRefreshListener(() -> {
            // 在这里实现下拉刷新的逻辑
            // 刷新完成后调用 swipeRefreshLayout.setRefreshing(false);
            swipeRefreshLayout.setRefreshing(false);
        });

        return view;
    }

    private void initNewsData() {
        newsList = new ArrayList<>();
        // 示例数据，你可以根据实际情况替换
        newsList.add(new News( "人工智能标准化白皮书发布，推动产业高质量发展", "近日，中国电子技术标准化研究院发布了《人工智能标准化白皮书(2025)》，系统梳理了人工智能标准化进展，分析了当前面临的挑战，并提出了未来发展建议。", "2025-06-15", 952, "另一条新闻的内容"));
        newsList.add(new News("另一条新闻标题", "另一条新闻的摘要内容", "2025-06-16", 123, "另一条新闻的内容"));
    }
}